// src/router/index.js
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import { useUserInfoStore } from '@/store/modules/user_info/index';
import {useIsShowLogin} from "@/store/modules/isShowLogin/index.js"
import i18n from '@/utils/i18n'; // 导入 i18n 实例


const routes = [
    // 定义基础路由，这些可以考虑静态导入，因为它们很可能是用户首次访问的页面
    {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        children: [
            {
                path: '',
                name: 'index',
                component: () => import('@/views/Home/Index/index.vue'),
                meta: { title: '歪歪兔' }
            },
            {
                path: 'reserve',
                name: 'Reserve',
                component: () => import('@/views/Home/Reserve/index.vue'),
                meta: { title: '新游推薦' }
            },
            {
                path: 'ranks',
                name: 'Ranks',
                component: () => import('@/views/Home/Ranks/index.vue'),
                meta: { title: '熱度榜' }
            },
            {
                path: 'announcement',
                name: 'Announcement',
                component: () => import('@/views/Home/Announcement/index.vue'),
                meta: { title: '最新公告' },
                children: [
                    {
                        path: '',
                        name: 'AnnouncementList',
                        component: () => import('@/views/Home/Announcement/AnnouncementList/index.vue'),
                        meta: { title: '最新公告' },
                    },
                    {
                        path: 'AnnouncementDetails/:id',
                        name: 'AnnouncementDetails',
                        component: () => import('@/views/Home/Announcement/AnnouncementDetails/index.vue'),
                        meta: { title: '公告詳情' },
                    }
                ]
            },
            {
                path: 'strategy',
                name: 'Strategy',
                // component: () => import('@/views/Home/Strategy/index.vue'),
                redirect: '/strategyDetails',
                meta: { title: '攻略' },
                children: [
                    {
                        path: 'strategyDetails/:id',
                        name: 'StrategyDetails',
                        component: () => import('@/views/Home/Strategy/StrategyDetails/index.vue'),
                        meta: { title: '攻略詳情' },
                    }
                ]
            },
            {
                path: 'ActivityDetails/:id',
                name: 'ActivityDetails',
                component: () => import('@/views/Home/ActivityDetails/index.vue'),
                meta: { title: '活動詳情' },
            },
            {
                path: 'hotActivity',
                name: 'HotActivity',
                component: () => import('@/views/Home/HotActivity/index.vue'),
                meta: { title: '活動中心' }
            },
            {
                path: 'gameCenter/:id',
                name: 'GameCenter',
                component: () => import('@/views/Home/GameCenter/index.vue'),
                meta: { title: '游戲中心' }
            },
            {
                path: 'sandSculpture',
                name: 'SandSculpture',
                component: () => import('@/views/Home/SandSculpture/index.vue'),
                meta: { title: '休閒劇場' },
            },
            {
                path: 'SandSculptureDetail',
                name: 'SandSculptureDetail',
                component: () => import('@/views/Home/SandSculpture/SandSculptureDetail/index.vue'),
                meta: { title: '休閒劇場' }
            },
            {
                path: 'SandSculptureMain',
                name: 'SandSculptureMain',
                component: () => import('@/views/Home/SandSculpture/SandSculptureMain/index.vue'),
                meta: { title: '休閒劇場' }
            },
        ]
    },
    {
        path: '/paymentSuccess',
        name: 'PaymentSuccess',
        component: () => import('@/views/PaymentSuccess/index.vue'),
        meta: { title: '支付結果' }
    },
    {
        path: '/paymentCancel',
        name: 'paymentCancel',
        component: () => import('@/views/paymentCancel/index.vue'),
        meta: { title: '支付取消' }
    },
    {
        path: '/platform_pay',
        name: 'Platform_pay',
        component: () => import('@/views/Platform_pay/index.vue'),
        meta: { title: '支付驗證' }
    },
    {
        path: '',
        redirect: '/home'
    },
    {
        path: '/allGames',
        name: 'Allgames',
        component: () => import('@/views/Allgames/index.vue'),
        meta: { title: '全部游戲' }
    },
    {
        path: '/giftPack/:id',
        name: 'GiftPack',
        component: () => import('@/views/GiftPack/index.vue'),
        meta: { title: '虛寶' }
    },
    {
        path: '/paymentPage',
        name: 'paymentPage',
        component: () => import('@/views/PaymentPage/index.vue'),
        meta: { title: '支付' }
    },
    {
        path: '/recharge',
        name: 'Recharge',
        component: () => import('@/views/Recharge/index.vue'),
        meta: { title: '儲值' },
        children: [
            {
                path: '',
                name: 'Recharge_index',
                component: () => import('@/views/Recharge/Index/index.vue'),
                meta: { title: '儲值' }
            },
            {
                path: 'gameRecharge',
                name: 'GameRecharge',
                component: () => import('@/views/Recharge/GameRecharge/index.vue'),
                meta: { title: '遊戲儲值' }
            },
            {
                path: 'svip',
                name: 'SVIP',
                component: () => import('@/views/Recharge/SVIP/index.vue'),
                meta: { title: 'SVIP' }
            },
            {
                path: 'economical',
                name: 'Economical',
                component: () => import('@/views/Recharge/Economical/index.vue'),
                meta: { title: '月卡' }
            },
            {
                path: 'rechargeRecords',
                name: 'RechargeRecords',
                component: () => import('@/views/Recharge/RechargeRecords/index.vue'),
                meta: { title: '儲值記錄' }
            },
            {
                path: 'collectionRecord',
                name: 'CollectionRecord',
                component: () => import('@/views/Recharge/CollectionRecord/index.vue'),
                meta: { title: '月卡領取記錄' }
            },
            {
                path: 'coupon',
                name: 'Coupon',
                component: () => import('@/views/Recharge/Coupon/index.vue'),
                meta: { title: '優惠券' }
            },
            
        ]
    },
    
    {
        path: '/userCenter',
        name: 'UserCenter',
        component: () => import('@/views/UserCenter/index.vue'),
        children: [
            {
                path: '',
                name: 'Coupon_index',
                component: () => import('@/views/UserCenter/Home/index.vue'),
                meta: { title: '個人中心' }
            },
            {
                path: 'modifyingInformation',
                name: 'ModifyingInformation',
                component: () => import('@/views/UserCenter/ModifyingInformation/index.vue'),
                meta: { title: '我的资料' }
            },
            {
                path: 'giftPack',
                name: 'user_GiftPack',
                component: () => import('@/views/UserCenter/GiftPack/index.vue'),
                meta: { title: '我的虛寶' }
            },
            {
                path: 'pointsLottery',
                name: 'pointsLottery',
                component: () => import('@/views/UserCenter/PointsLottery/index.vue'),
                meta: { title: '積分抽獎' }
            },
            {
                path: 'pointsMall',
                name: 'pointsMall',
                component: () => import('@/views/UserCenter/PointsMall/index.vue'),
                meta: { title: '積分商城' }
            },
            {
                path: 'integralRecord',
                name: 'IntegralRecord',
                component: () => import('@/views/UserCenter/IntegralRecord/index.vue'),
                meta: { title: '積分記錄' }
            },
            {
                path: 'gamed',
                name: 'Gamed',
                component: () => import('@/views/UserCenter/Gamed/index.vue'),
                meta: { title: '我的游戲' }
            },
            {
                path: 'watchHistory',
                name: 'WatchHistory',
                component: () => import('@/views/UserCenter/WatchHistory/index.vue'),
                meta: { title: '觀看曆史' }
            },
            {
                path: 'evaluate',
                name: 'Evaluate',
                component: () => import('@/views/UserCenter/Evaluate/index.vue'),
                meta: { title: '評價' }
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login/index.vue'),
        meta: { title: '賬號登錄' }
    },
    {
        path: '/fb_login',
        name: 'fb_Login',
        component: () => import('@/views/fb_login/index.vue'),
        meta: { title: 'faceBook登錄' }
    },
    {
        path: '/fb_binding',
        name: 'fb_binding',
        component: () => import('@/views/fb_binding/index.vue'),
        meta: { title: '綁定faceBook' }
    },
    {
        path: '/yh_login',
        name: 'yh_Login',
        component: () => import('@/views/yh_login/index.vue'),
        meta: { title: 'Yahoo登錄' }
    },
    {
        path: '/yh_binding',
        name: 'yh_binding',
        component: () => import('@/views/yh_binding/index.vue'),
        meta: { title: '綁定Yahoo' }
    },
    {
        path: '/gg_login',
        name: 'gg_Login',
        component: () => import('@/views/gg_login/index.vue'),
        meta: { title: 'Google登錄' }
    },
    {
        path: '/gg_binding',
        name: 'gg_binding',
        component: () => import('@/views/gg_binding/index.vue'),
        meta: { title: '綁定Google' }
    },
    {
        path: '/footer',
        name: 'Footer',
        children: [
            {
                path: 'privacyPolicy',
                name: 'PrivacyPolicy',
                component: () => import('@/views/Footer/PrivacyPolicy/index.vue'),
                meta: { title: '隱私政策' }
            },
            {
                path: 'serviceTerms',
                name: 'ServiceTerms',
                component: () => import('@/views/Footer/ServiceTerms/index.vue'),
                meta: { title: '服務條款' }
            },
            {
                path: 'about',
                name: 'About',
                component: () => import('@/views/Footer/About/index.vue'),
                meta: { title: '關於我們' }
            }
        ]
    },
    {
        path: '/playgame',
        name: 'Playgame',
        component: () => import('@/views/Playgame/index.vue'),
        meta: { title: '加載中' }
    },
    {
        path: '/:catchAll(.*)', // 匹配所有未定义的路由
        name: 'NotFound',
        component: () => import('@/views/NotFound/index.vue'),
    },

];

const router = createRouter({
    history: createWebHashHistory(), // 使用Hash模式.url带#
    routes,
})




// 添加全局前置守卫
router.beforeEach((to, from, next) => {
    // 根据路由元信息或名称动态设置title
    // document.title = to.meta.title || to.name || 'game';
    const title = to.meta.title ? to.meta.title : '歪歪兔';
    document.title = to.meta.title ? i18n.global.t(to.meta.title) : title;
    // console.log(to);
    const user_infoStore = useUserInfoStore();
    const user_info = reactive(user_infoStore.userInfo)
    //没登录个人中心、储值重定向首页

    
    if(user_info.status == 3 && to.path != '/home'){
        window.location.href = window.location.origin
    }
    
    if (to.matched[0].path === '/userCenter' || to.matched[0].path === '/recharge') {
        // 检查路由是否需要认证
        if (!user_infoStore.userToken) {
            // 如果没有 token，则弹出登录框
            if(window.location.hash !== '#/home'){
                const isShowLoginStore = useIsShowLogin();
                isShowLoginStore.showLogin()
            }else{
                if(localStorage.getItem('lang') == 'en'){
                    ElMessage.warning('Please log in first!')
                }else{
                    ElMessage.warning('請先登錄！')
                }
                
            }
            
            
            // next({ name: 'index' });
        } else {
            next(); // 如果有 token，继续路由跳转
        }
    } else {
        next(); // 不需要认证的路由直接放行
        
    }
    // next();
});

router.afterEach(() => {
    window.scrollTo(0, 0); // 跳转完成后将滚动条位置设置到顶部
});

export default router